package com.huaruan.youdi.views.activitys;

import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.View;
import android.widget.TextView;


import com.huaruan.youdi.R;
import com.huaruan.youdi.adapter.MainPagerAdapter;
import com.huaruan.youdi.http.okhttp.Constant;
import com.huaruan.youdi.http.okhttp.HttpCallback;
import com.huaruan.youdi.http.okhttp.HttpUtils;
import com.huaruan.youdi.utils.AppManager;
import com.huaruan.youdi.utils.AppUtils;
import com.huaruan.youdi.utils.StringUtils;
import com.huaruan.youdi.utils.UserUtils;
import com.huaruan.youdi.views.MainTabViewPager;
import com.huaruan.youdi.views.WHImageView;
import com.huaruan.youdi.views.fragments.BaseFragment;
import com.huaruan.youdi.views.fragments.MainFragment;
import com.huaruan.youdi.views.fragments.MineFragment;

import java.util.ArrayList;
import java.util.SortedMap;
import java.util.TreeMap;

import butterknife.BindView;
import cn.jpush.android.api.JPushInterface;

public class MainActivity extends BaseActivity {
    @BindView(R.id.viewPager)
    public MainTabViewPager viewPager;
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    static final String[] TITLES = new String[]{"业务", "我的"};
    static final int[] ICONS = new int[]{R.drawable.selector_main_tab_background_one,
            R.drawable.selector_main_tab_background_two};
    ArrayList<BaseFragment> fragments;

    @Override
    public int IGetContentViewResId() {
        return R.layout.activity_main;
    }

    @Override
    public void IFindViews() {
        viewPager.setScrollHorizonal(false);
        viewPager.setAnimation(false);
        viewPager.setOffscreenPageLimit(TITLES.length - 1);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        fragments = new ArrayList<>();
        final MainFragment mainFragment = new MainFragment();
        final MineFragment mineFragment = new MineFragment();
        fragments.add(mainFragment);
        fragments.add(mineFragment);
        final FragmentPagerAdapter adapter = new MainPagerAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab != null)
                tab.setCustomView(getTabView(i));
        }

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                WHImageView iv_photo = tab.getCustomView().findViewById(R.id.iv_tab);
                int position = tab.getPosition();
                iv_photo.setImageResource(ICONS[position]);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                WHImageView iv_photo = tab.getCustomView().findViewById(R.id.iv_tab);
                int position = tab.getPosition();
                iv_photo.setImageResource(ICONS[position]);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
        viewPager.setCurrentItem(0);

    }

    @Override
    public void IInitData() {
    }

    @Override
    public void IRequest() {
        String registrationID = JPushInterface.getRegistrationID(this);
        if (!StringUtils.isEmpty(registrationID)) {
            insertJPush(registrationID);
        }
    }

    private void insertJPush(String registrationId) {
        SortedMap<String, String> mSortedMap = new TreeMap<>();
        mSortedMap.put("userId", UserUtils.getUid());
        mSortedMap.put("registrationId", registrationId);
        new HttpUtils.Builder(IGetContext())
                .postMethod()
                .url(Constant.INSERT_PUSH)
                .params(mSortedMap)
                .callback(new HttpCallback() {
                    @Override
                    public void success(Object object) {
                    }
                }).start();
    }


    public View getTabView(final int position) {
        View indicator = getLayoutInflater().inflate(R.layout.main_tab, null);
        WHImageView iv_tab = indicator.findViewById(R.id.iv_tab);
        iv_tab.setImageResource(ICONS[position]);
        TextView tv_tab = (TextView) indicator.findViewById(R.id.tv_tab);
        tv_tab.setText(TITLES[position]);
        return indicator;
    }

    long time = 0;

    @Override
    public void onBackPressed() {
        long t = System.currentTimeMillis();
        if (t - time <= 2 * 1000) {
            AppManager.getAppManager().AppExit();
        } else {
            time = System.currentTimeMillis();
            AppUtils.toastShort(IGetContext(), "再按一次退出24优递");
        }
    }

    @Override
    public boolean HideStatusBar() {
        return true;
    }
}
